HAProxy : HTTP Load Balancing
2015/01/22 |
Install HAProxy to configure Load Balancing Server.
This example based on the environment like follows. | -------+----------------------------------------------- | +-------------------+--------------------+ |10.0.0.30 |10.0.0.31 |10.0.0.32 +-----+-----+ +-------+------+ +-------+------+ | Frontend | | Backend#1 | | Backend#2 | | HAProxy | | Web Server | | Web Server | +-----------+ +--------------+ +--------------+ |
Configure Servers that HTTP connection to HAProxy Server is forwarded to backend Web Servers.
|
|
[1] | Install HAProxy. |
[root@dlp ~]# yum -y install haproxy
|
[2] | Configure HAProxy. |
[root@dlp ~]# mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.org
[root@dlp ~]#
vi /etc/haproxy/haproxy.cfg # create new global # for logging section log 127.0.0.1 local2 info chroot /var/lib/haproxy pidfile /var/run/haproxy.pid # max per-process number of connections maxconn 256 # process' user and group user haproxy group haproxy # makes the process fork into background daemon defaults # running mode mode http # use global settings log global # get HTTP request log option httplog # timeout if backends do not reply timeout connect 10s # timeout on client side timeout client 30s # timeout on server side timeout server 30s # define frontend ( set any name for "http-in" section ) frontend http-in # listen 80 bind *:80 # set default backend default_backend backend_servers # send X-Forwarded-For header option forwardfor # define backend backend backend_servers # balance with roundrobin balance roundrobin # define backend servers server www01 10.0.0.31:80 check server www02 10.0.0.32:80 check /etc/rc.d/init.d/haproxy start Starting haproxy: [ OK ] [root@dlp ~]# chkconfig haproxy on |
[3] | Configure Rsyslog to get logs for HAProxy. |
[root@dlp ~]#
vi /etc/rsyslog.conf # line 13,14: uncomment, lne 15: add $ModLoad imudp $UDPServerRun 514
$AllowedSender UDP, 127.0.0.1
# line 42: change like follows *.info;mail.none;authpriv.none;cron.none ,local2.none /var/log/messageslocal2.* /var/log/haproxy.log /etc/rc.d/init.d/rsyslog restart |
[4] | Change httpd settings on Backends to logging X-Forwarded-For header. |
[root@www ~]#
vi /etc/httpd/conf/httpd.conf # line 497: change like follows LogFormat " \"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
/etc/rc.d/init.d/httpd restart |
[5] | Make sure all works fine to access to the frontend server from a Client with HTTP like follows. |